import { Router } from 'express'
const router = Router()
import config from 'config'
import mysql from '../libs/mysql'
import _ from 'lodash'
import md5 from 'md5'
// Mock Users
const users = [
  { name: 'Alexandre' },
  { name: 'Pooya' },
  { name: 'Sébastien' },
]

/* GET users listing. */
router.get('/users', function (req, res, next) {
  res.json(users)
})

/* GET user by ID. */
router.get('/users/:id', function (req, res, next) {
  const id = parseInt(req.params.id)
  if (id >= 0 && id < users.length) {
    res.json(users[id])
  } else {
    res.sendStatus(404)
  }
})
router.post('/create/user',async (req,res,next)=>{
  let xx = config.app;
  let x = req.body;
  // var connection = mysql.createConnection({     
  //   host     : 'localhost',       
  //   user     : 'root',              
  //   password : '',       
  //   port: '3306',                   
  //   database: 'myblog', 
  // });
  // connection.connect(function(err){
  //   if(err) throw err;
  //   console.log('connected mysql')
  // });
  // connection.query('select * from users',function(error, results, fields){
  //   connection.end();
  //   console.log(results);
  //   return res.send('xxx'+xx);
  // })  
  let con = await mysql.getConnect();
  let sql = `select * from users where name = ? and sex = ?`;
  let dbresult = await mysql.query(con,sql,'guojian',1);
  let release = await mysql.release(con);
  return res.send(dbresult);
})
export default router
